<template>
  <div>
    <el-form>
      <el-row>
        <el-col :span="24" style="margin: 10px;">
          <el-form-item label="请选择退回节点："/>
          <el-radio-group v-model="formParams.nextNodeId" @change="chooseNode">
            <el-radio style="margin: 5px;" v-for="node in backNodeList"
                      :label="node.Nodeid"
                      :key="node.Nodeid">
              {{ node.NodeName }}
            </el-radio>
          </el-radio-group>
        </el-col>
        <el-col :span="24">
          <el-form-item label="节点审批人" label-width="90px">
            <el-tag :key="formParams.nextUserList">{{ formParams.userName }}</el-tag>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="退回意见" label-width="80px"/>
          <el-input type="textarea" maxlength="300" v-model="formParams.remark" placeholder="请输入退回意见"
                    :autosize="{ minRows: 4 }"
                    show-word-limit></el-input>
        </el-col>
        <el-col :span="24" style="margin: 10px;">
          <el-button type="primary" size="small" @click="submit">提 交</el-button>
          <el-button size="small" @click="cancel">取 消</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
  import {anyNode} from "@/api/workflow/workFlow";

  export default {
    props: {
      // 退回节点列表
      backNodeList: Array,
      // 流程参数
      processParams: Object,
      // 历史记录列表
      historyList: Array,
      // 节点是否有路由条件
      isRouting: Boolean
    },

    data() {
      return {
        formParams: {
          // 退回节点id
          nextNodeId: undefined,
          // 节点名称
          nextNodeName: undefined,
          // 节点处理人id
          nextUserList: undefined,
          // 节点处理人
          userName: undefined,
          // 退回意见
          remark: ''
        }
      }
    },

    methods: {
      chooseNode(val) {
        // 初始化
        this.formParams.nextUserList = undefined
        this.formParams.userName = undefined
        this.formParams.nextNodeName = undefined

        // 反向遍历历史记录，遍历的第一条记录为该节点最近处理人
        for (let i = this.historyList.length - 1; i > -1; i--) {
          if (val === this.historyList[i].Nodeid) {
            this.formParams.nextUserList = this.historyList[i].Userid
            this.formParams.userName = this.historyList[i].UserName
            this.formParams.nextNodeName = this.historyList[i].NodeName
            break
          }
        }
      },

      submit() {
        if (!this.formParams.nextNodeId) {
          this.$message.error('请选择节点后再提交')
        }
        // 如果节点有路由条件需要进行设置
        if (this.isRouting) {
          if (this.formParams.nextNodeId === 'T10004') {
            this.formParams.formData = JSON.stringify({'hasInspectors': '1'})
          } else if (this.formParams.nextNodeId === 'T10005') {
            this.formParams.formData = JSON.stringify({'hasInspectors': '0'})
          } else {
            this.formParams.formData = JSON.stringify({})
          }
        } else {
          this.formParams.formData = JSON.stringify({})
        }
        // 回退参数设置
        this.formParams.taskId = this.processParams.taskId
        this.formParams.startTime = this.processParams.startTime
        this.formParams.currentNodeId = this.processParams.currentNodeId
        this.formParams.currentNodeName = this.processParams.currentNodeName
        this.formParams.businessId = this.processParams.businessId
        this.formParams.docUnid = this.processParams.docUnid
        this.formParams.processId = this.processParams.processId
        this.formParams.processName = this.processParams.processName
        this.formParams.link = this.processParams.link
        // 回退
        anyNode(this.formParams).then(response => {
          if (response.retCode !== '200') {
            this.msgError(response.message)
            return
          }
          this.msgSuccess(response.message);
          this.$emit('submitBackPage')
        })
      },

      cancel() {
        this.$emit('closeBackPage')
      }
    }
  }
</script>